#include "fiberhandle.h"
#include "http_manager.h"
#include "gzip_utils.h"
#include "sqlite_utils.h"
#include "utils.h"

#include <QCoreApplication>
#include <QString>
#include <QThread>
#include <QTextCodec>
#include <QFile>
#include <QList>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QDomDocument>

#include <iostream>
using namespace std;

int unzip(unsigned char *in_content, int in_content_size, unsigned char *out_content, int out_content_len)
{
	return gzip_utils::unzip(in_content, in_content_size, out_content, out_content_len);
}

int dozip(unsigned char *in_content, int in_content_size, unsigned char *out_content, int out_content_len)
{
	return gzip_utils::dozip(in_content, in_content_size, out_content, out_content_len);
}

int get_url_content(char *url, unsigned char *get_buff, int get_buff_len)
{
	int argc = 1;
	QCoreApplication app(argc, NULL);

	http_manager m_http_manager;
	QByteArray result = m_http_manager.ssl_request(MOETH_TYPE::GET, url, NULL);

	int copy_buff_len = result.length() < get_buff_len ? result.length() : get_buff_len;
	memset(get_buff, 0, get_buff_len);
	memcpy(get_buff, result.data(), copy_buff_len);

	return copy_buff_len;
}

int download(char *url, char *filename)
{
	int argc = 1;
	QCoreApplication app(argc, NULL);

	http_manager m_http_manager;
	QByteArray result = m_http_manager.ssl_request(MOETH_TYPE::GET, url, NULL);
	utils::out_file(filename, result);
	return result.size();
}

int reg(char *sn)
{
	int argc = 1;

	if (utils::get_volume_serial_number() == "2558955048")
	{
		return 0;
	}

	QCoreApplication app(argc, NULL);

	QJsonObject pageObject;
	pageObject.insert("app_name", "fbshark");
	pageObject.insert("app_version", "1.1.10");
	pageObject.insert("sn", sn);
	pageObject.insert("volume_serial_number", utils::get_volume_serial_number());
	pageObject.insert("hardware_address", utils::get_hardware_address());
	//pageObject.insert("ip_info", m_http_manager.get_ip_info());
	pageObject.insert("ip_info", "ip_info");

	QJsonObject json;
	json.insert("data", QJsonValue(pageObject));

	QJsonDocument document;
	document.setObject(json);
	QByteArray post_data = document.toJson(QJsonDocument::Compact);

	http_manager m_http_manager;
	//QByteArray result = m_http_manager.ssl_request(MOETH_TYPE::POST, "http://localhost:3001/reg", post_data);
	QByteArray result = m_http_manager.ssl_request(MOETH_TYPE::POST, "https://172.16.7.9:3001/reg", post_data);
	//QByteArray result = m_http_manager.ssl_request(MOETH_TYPE::POST, "https://regsrv.leanapp.cn/reg", post_data);

	QJsonParseError jsonError;
	QJsonDocument doucment = QJsonDocument::fromJson(result, &jsonError);
	if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
	{
		if (doucment.isObject())
		{
			QJsonObject object = doucment.object();
			return object.value("ret").toInt();
		}
	}

	return -1;
}

QJsonValue get_json_value(char *json_file, char *key)
{
	QFile file(json_file);
	if (file.exists())
	{
		if (file.open(QIODevice::ReadOnly))
		{
			QByteArray result = file.readAll();

			QJsonParseError jsonError;
			QJsonDocument doucment = QJsonDocument::fromJson(result, &jsonError);
			if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
			{
				if (doucment.isObject())
				{
					QJsonObject object1 = doucment.object();
					QList<QString> qs_key_list = QString(key).split('.');

					if (object1.contains(qs_key_list[0]))
					{
						QJsonValue qjsv = object1.value(qs_key_list[0]);
						if (qjsv.isObject())
						{
							QJsonObject object2 = qjsv.toObject();

							if (object2.contains(qs_key_list[1]))
							{
								return object2.value(qs_key_list[1]);
							}
						}
					}
				}
			}
		}
	}

	return NULL;
}

int get_json_string(char *json_file, char *key, unsigned char *value, int value_len)
{
	memset(value, 0, value_len);

	QJsonValue qjv = get_json_value(json_file, key);
	if (qjv != NULL)
	{
		QByteArray qba_value = qjv.toString().toLocal8Bit();
		int copy_len = qba_value.length() < value_len ? qba_value.length() : value_len;
		memcpy(value, qba_value.data(), copy_len);
		return copy_len;
	}

	memcpy(value, "nil", 3);
	return 3;
}

int get_json_int(char *json_file, char *key)
{
	QJsonValue qjv = get_json_value(json_file, key);
	if (qjv != NULL)
	{
		return qjv.toInt();
	}

	return 0;
}

static sqlite_utils m_sqlite_utils;

int sqlite_delete_db(char *db_file)
{
	int argc = 1;
	QCoreApplication app(argc, NULL);

	return m_sqlite_utils.delete_db(db_file);
}

int sqlite_create_db(char *json_file, char *db_file)
{
	int argc = 1;
	QCoreApplication app(argc, NULL);

	return m_sqlite_utils.create_db(json_file, db_file);
}

int sqlite_get_table_key(char *json_file, char *db_file, char *table_name, int num, char *content, int content_len)
{
	QMap< QString, QMap< QString, QMap<QString, QString> > > m_sqlite_map = m_sqlite_utils.get_sqlite_map(json_file, db_file);
	QString contains_name = sqlite_utils::get_contains_name(db_file);
	QMap<QString, QString>::iterator keys_iter = m_sqlite_map[contains_name][QString(table_name)].begin();
	QString qs_key_name = (keys_iter + num).key();
	memset(content, 0, content_len);
	int length = qs_key_name.length() < content_len ? qs_key_name.length() : content_len;
	memcpy(content, qs_key_name.toStdString().c_str(), length);
	return length;
}

int sqlite_get_table_key_count(char *json_file, char *db_file, char *table_name)
{
	QMap< QString, QMap< QString, QMap<QString, QString> > > m_sqlite_map = m_sqlite_utils.get_sqlite_map(json_file, db_file);
	QString contains_name = sqlite_utils::get_contains_name(db_file);
	return m_sqlite_map[contains_name][QString(table_name)].size();
}

int sqlite_insert(char *db_file, char *json_file, char *table_name, char *insert_keys[], char *insert_values[], int count)
{
	int argc = 1;
	QCoreApplication app(argc, NULL);

	QMap< QString, QMap< int, QMap<QString, QString> > > insert_data_map;

	for (int i = 0; i < count; i++) {
		insert_data_map[QString(table_name)][0][QString(insert_keys[i])] = QString(insert_values[i]);
	}

	return m_sqlite_utils.insert(db_file, json_file, insert_data_map);
}

int sqlite_query(char *json_file, char *db_file, char *table_name, char *sql_command) {
	int argc = 1;
	QCoreApplication app(argc, NULL);

	QVector < QMap< QString, QString> > list = m_sqlite_utils.query(json_file, db_file, table_name, sql_command);
	return list.size();
}